package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.utils.CacheUtil;
import cn.tedu._05weibo.conmon.response.JsonResult;
import cn.tedu._05weibo.conmon.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "02.微博模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    /*@Autowired
    private RedisTemplate<String, Serializable> redisTemplate;*/
    @Autowired
    private CacheUtil cacheUtil;//自动装配自己创建的缓存工具类

    //发布微博
    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public JsonResult insertWeibo(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession httpSession){
        UserVO userVO=(UserVO) httpSession.getAttribute("user");
        Weibo weibo = new Weibo();
        weibo.setContent(weiboDTO.getContent());
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }

    @ApiOperation(value = "微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        /*
        设计redis中首页数据结构{"weibo_index":List<WeiboIndexVO>}
        先到redis中查看是否存在首页缓存数据；
        如果存在，直接返回；
        如果不存在，查询数据库，将查询结果存入redis中，再返回；*/
        /*ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable weiboIndex = ops.get("weibo_index");*/

        Serializable weiboIndex = cacheUtil.get("weibo_index");
        if (weiboIndex==null){//不存在，查询数据库
            List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndex();
            log.error("首页数据来自mysql数据库");
            //ops.set("weibo_index",(Serializable) weiboIndexVOS,30, TimeUnit.SECONDS);//将查询结果存入redis中

            //使用缓存工具类方法将查询结果存入redis中
            cacheUtil.setExpire("weibo_index",(Serializable) weiboIndexVOS,30);
            return JsonResult.ok(weiboIndexVOS);
        }
        log.error("首页数据来自redis缓存");
        return JsonResult.ok(weiboIndex);
    }

    @ApiOperation(value = "微博详情页")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id",value = "微博编号",dataType = "Long",required = true)
    public JsonResult selectById(@Range(min = 1,max=500,
            message = "微博ID必须在1到500之间") @RequestParam Long id){
        if (id<0){
            throw new IllegalArgumentException("微博编号不能小于0");
        }
        WeiboDetailVO weiboDetailVO= weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
